#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
from tools import *

# 第一个接受路径作为可选参数
# 路径中必须包含 32 个网络
# 参数结尾必须是数字, 如: trian_net/3
# 会接着上次继续训练
# 第二个参数是本次训练的代数
# 第三个参数是保存间隔

# 初始化 32 位选手
players = list()
gen = 0
step = 1
savs = 1
if len(sys.argv) > 1:
    root = sys.argv[1]
    gen = int(os.path.split(root)[-1]) + 1
    for f in os.listdir(root):
        players.append(joblib.load(os.path.join(root, f)))
else:
    for i in range(0, 32):
        players.append(Gomoku())

if len(sys.argv) > 2:
    step = int(sys.argv[2])

if len(sys.argv) > 3:
    savs = int(sys.argv[3])

# 遗传 20 次
directory = "train_net"
if not os.path.isdir(directory):
    os.mkdir(directory)

for i in range(gen, gen + step):
    print("ga: %d start:" % i)
    players = ga32(players)
    if i % savs == 0:
        path = '%s%s%d' % (directory, os.sep, i)
        os.mkdir(path)
        print("saving...")
        for j in range(0, 32):
            players[j].save('%s%s%d.net' % (path, os.sep, j))
    print("ga: %d end:" % i)
